//--- Projeto Domus
// interactionsource.com
// Koji Pereira, Andre Veloso e Marcos Paulo

/*
 Projeto Domus
 interactionsource.com
 Koji Pereira, Andre Veloso e Marcos Paulo

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


package com.interactionsource.input.wiimoteosc;

/*
 */

import java.io.IOException;
import java.util.EventListener;

import org.wiigee.control.Wiigee;
import org.wiigee.event.AccelerationListener;
import org.wiigee.event.ButtonListener;
import org.wiigee.event.GestureListener;
import org.wiigee.filter.DirectionalEquivalenceFilter;
import org.wiigee.filter.Filter;
import org.wiigee.filter.HighPassFilter;
import org.wiigee.filter.IdleStateFilter;
import org.wiigee.filter.MotionDetectFilter;

import com.interactionsource.event.ContinuousEventListener;
import com.interactionsource.event.TriggerEventListener;


// Singleton
public class WiimoteOSCWiigee extends Wiigee {

    protected static String pluginversion = "1.5.6";
    protected static String pluginreleasedate = "20090817";
    private static final Object lock = new Object();
    
    private WiimoteOSC devices;
    
    private WiimoteOSCStreamer wiimoteOSCStreamer;

    private int OSCPort;
    
    private MotionDetectFilter motionDetectFilter;
    private HighPassFilter highPassFilter;
    
    public WiimoteOSCWiigee() {
        super();

    }

    public WiimoteOSCWiigee(int listenPort) {
        super();
        OSCPort = listenPort;
        
        
        try {
        	//Do not filter accell events
        	this.devices = new WiimoteOSC(OSCPort, true);
        	
        	this.wiimoteOSCStreamer = new WiimoteOSCStreamer(this.devices);
        	
        	
        	//Rearranja os filtros que sao adcionados ao device
        	//para melhor resultado do processo de analise
        	
        	devices.resetAccelerationFilters();
        	//devices.setAccelerationEnabled(true);

        	motionDetectFilter = new MotionDetectFilter(this.devices);
        	motionDetectFilter.setMotionChangeTime(50);
        	System.out.println("WiimoteOSCWiigee - motionDetectFilter tempo:" + motionDetectFilter.getMotionChangeTime());

        	highPassFilter = new HighPassFilter(0.1);
        	
        	this.devices.addAccelerationFilter(new IdleStateFilter());
        	this.devices.addAccelerationFilter(motionDetectFilter);
        	this.devices.addAccelerationFilter(new DirectionalEquivalenceFilter());
        	this.devices.addAccelerationFilter(highPassFilter);
        	
        	
        } catch (IOException e1) {
            e1.printStackTrace();
            return;       	
        	
        }
    }    
    
	public void addButtonListener(ButtonListener listener) {
		devices.addButtonListener(listener);
	}
    
	public void addGestureListener(GestureListener listener) {
		devices.addGestureListener(listener);
	}
	
	public void addAccelerationListener(AccelerationListener listener) {
		devices.addAccelerationListener(listener);
	}	
	
	public void addFilter(Filter filter) {
		devices.addAccelerationFilter(filter);
	}
	
	//novos listeners para os novos gesto implementados
	public void addPulseListener(TriggerEventListener listener) {
		
		wiimoteOSCStreamer.addPulseListener(listener);
		
	}
	
	public void addGlobalForceListener(ContinuousEventListener listener) {
		
		wiimoteOSCStreamer.addGlobalForceListener(listener);
	}
	
	
	public WiimoteOSC getDevice() {
	    return devices;
	}
	
	
	/**
	 * Sets the Trainbutton for all wiimotes;
	 *
	 * @param b Button encoding, see static Wiimote values
	 */
	public void setTrainButton(int b) {
		devices.setTrainButton(b);
	}
	
	/**
	 * Sets the Recognitionbutton for all wiimotes;
	 *
	 * @param b Button encoding, see static Wiimote values
	 */
	public void setRecognitionButton(int b) {
		devices.setRecognitionButton(b);
	}
	
	/**
	 * Sets the CloseGesturebutton for all wiimotes;
	 *
	 * @param b Button encoding, see static Wiimote values
	 */
	public void setCloseGestureButton(int b) {
		devices.setCloseGestureButton(b);
	}    
    
    public void mostraSaida() {
    		devices.mostraSaida();
    	
    }
    
    public void salvaGesto(int id, String nomeArquivo) {
    	devices.getProcessingUnit().saveGesture(id, nomeArquivo);
    	
    }
  
    public void carregaGesto(String nomeArquivo) {
    	devices.getProcessingUnit().loadGesture(nomeArquivo);
    	
    }
    
	public void iniciaTreinoGesto( int button ) {
		this.wiimoteOSCStreamer.simulateButtonPressed(button);
		
	}
	
	public void terminaTreinoGesto ( int button ) {
		this.wiimoteOSCStreamer.simulateButtonReleased(button);
		
	}
	
	public void salvaTreinoGesto ( int button ) {
		this.wiimoteOSCStreamer.simulateButtonPressed(button);
		
	}

	public void iniciaReconhecimentoGesto ( int button ) {
		this.wiimoteOSCStreamer.simulateButtonPressed(button);		
		
	}
	
	public void terminaReconhecimentoGesto( int button ) {
		this.wiimoteOSCStreamer.simulateButtonReleased(button);		
		
	}
    
    public int getRecognitionButton() {
    	return this.devices.getRecognitionButton();
    	
    }
    
    public int getCloseGestureButton() {
    	return this.devices.getCloseGestureButton();
    	
    }
    
    public int getTrainButton() {
    	return this.devices.getTrainButton();
    	
    }

	/**
	 * @return the oSCPort
	 */
	public int getOSCPort() {
		return OSCPort;
	}

	/**
	 * @param oSCPort the oSCPort to set
	 */
	public void setOSCPort(int oSCPort) {
		this.OSCPort = oSCPort;
		
		devices.setOSCPort(this.OSCPort);
		try {
			this.devices = new WiimoteOSC(this.OSCPort, false);
			this.wiimoteOSCStreamer = new WiimoteOSCStreamer(this.devices);
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	
}

